package basic.thread.again;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**单利模式之饿汉式线程问题
 * @author bingo
 * @create 2018-02-18 18:38
 **/
public class SingleThread {

    private  static SingleThread singleThread;

    private SingleThread(){

    }

    public  static SingleThread singleThreadInstance(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(singleThread==null){
            synchronized (SingleThread.class){
                 singleThread=new SingleThread();
            }
        }

        return  singleThread;
    }

    public  static void main(String[] args) {

        ExecutorService pool = Executors.newFixedThreadPool(20);
        for(int i=0;i<20;i++){
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"---"+SingleThread.singleThreadInstance());
                }
            });
        }

    }
}
